<?php
/**
 * Created by PhpStorm.
 *
 * Author : Traveler.
 * Time   : 2020/3/10 16:33
 * Company: Inner Mongolia Zhengzhi Network Technology Co., Ltd
 */

namespace app\admin\controller;


use app\admin\BaseController;
use app\admin\model\IcafeWan as IcafeWanModel;
use app\admin\middleware\Login;
use think\facade\View;

class icafeWan extends BaseController
{
    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [Login::class];

    /**
     * 接口：获取专线信息(表格渲染)
     * @return \think\response\Json
     */
    public function getTableData()
    {
        $param = $this->request->param();
        $data  = IcafeWanModel::getIcafeWanTable($param);

        return $this->success('获取专线信息成功', $data['data'], ['count' => $data['count']]);
    }

    /**
     * 添加外网：专线
     * @return string|\think\response\Json
     */
    public function add()
    {
        // 提交
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $result = IcafeWanModel::create($data);
            if (!$result->isEmpty()) {
                return $this->success('添加成功');
            }

            return $this->error('添加失败');
        }

        // 渲染页面：添加外网：专线
        return View::fetch();
    }

    /**
     * 编辑外网：专线
     * @return string|\think\response\Json
     */
    public function edit()
    {
        // 提交
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $result = IcafeWanModel::update($data);
            if (!$result->isEmpty()) {
                return $this->success('编辑成功');
            }

            return $this->error('编辑失败');
        }

        // 渲染页面：编辑外网：专线
        return View::fetch();
    }

    /**
     * 批量删除
     * @return \think\response\Json
     */
    public function delete()
    {
        // 提交
        if ($this->request->isPost()) {
            $ids = trim($this->request->post('idsStr'), " \t\n\r\0\x0B,");

            $result = IcafeWanModel::whereIn('id', $ids)->delete();
            if ($result) {
                return $this->success('删除成功：' . $result . '条');
            }

            return $this->error('删除失败');
        }

        return $this->error('非法请求');
    }
}